public class poritytree {
    class Solution {
        int pindex = 0;
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            return buildTreechild(preorder,inorder,0,inorder.length-1);
        }

        public TreeNode buildTreechild(int[] preorder,int [] inorder,int bei,int bt){
            if(bei>bt){
                return null;
            }
            TreeNode root = new TreeNode(preorder[pindex]);
            int rootindex= find(inorder,bei,bt,preorder[pindex]);
            if(rootindex==-1){
                return null;
            }
            pindex++;
            root.left = buildTreechild(preorder,inorder,bei,rootindex-1);
            root.right = buildTreechild(preorder,inorder,rootindex+1,bt);
            return root;
        }

        public int find(int[] inorder,int bei,int bt,int key){
            for(int i =bei ; i<=bt; i++){
                if(inorder[i]==key){
                    return i;
                }
            }
            return -1;
        }
    }
}
